Ext.require(  
        [  
            'Ext/ux/XHtmlEditor'  
         ]          
);

summaryList = function(){
	
    Ext.define('summaryModel',{
        extend: 'Ext.data.Model',
        fields: [
            {name: 'summaryId',type: 'String'},
             {name: 'studentId',type: 'String'},
             {name: 'title',type: 'String'},
             {name: 'contentText', type: 'String'},
             {name: 'createDate', type: 'String'},
             {name: 'state',type: 'String'},
             {name:'remark',type:'String'},
             {name:'visible',type:'String'},
             {name:'academicYear',type:'String'},
             {name:'modifyDateLimit',type:'String'}
         ]
    });
	
    var isVisibleStore = getStaticDataStore('isVisible');
    
	var summaryStore = Ext.create('Ext.data.Store',{
		pageSize:8,
		model: summaryModel,
	    autoLoad:true,
	    proxy:{
	        type: 'ajax',
	        url: 'searchSummaryListAction.action',
	        reader:{
	           type: 'json',
	           root: 'resultList.list',            
	           totalProperty: 'resultList.total'
	        },
	        extraParam:{
	            	
	        }
	   }
	       
	   });
	
	//var checkBox = Ext.create('Ext.selection.CheckboxModel');
	 
 
   // var sm=new Ext.grid.CheckboxSelectionModel();
	var grid = Ext.create('Ext.grid.GridPanel',{
		store:summaryStore,
		columnLines:true,
		//selModel:checkBox,
		disableSelection:false,
		loadMask:true,
	
		columns:[
		         {xtype: 'rownumberer',text:'序号',width:40},
		         {text:'编号',width:'10%',dataIndex:'summaryId',sortable:true},
		         {text:'标题',width:'20%',dataIndex:'title',sortable:true},
		         {text:'内容',width:'45%',dataIndex:'contentText',hidden:true},
		         {text:"创建时间",width:'15%',dataIndex:'createDate',renderer : function(_registerDate){
		             return _registerDate.replace("T"," ").substring(0,16);
		         }},
		         {text:'备注',width:'10%',dataIndex:'remark'},
		         {text:'是否公开',width:60,dataIndex:'visible',renderer:function(v){
		        	/* if(v == '1'){
		        		 return '私有';
		        	 }else if(v == '2'){
		        		 return '公开';
		        	 }else{
		        		 return 'dd'; 
		        	 }*/
        			 var index = isVisibleStore.find('valueId',v); 
        			 if(index != -1){
        				 var r = isVisibleStore.getAt(index);
        				 return r.get("valueName");
        			 }
        			 else{
        				 return v;
        			 }
        	 } }
		         ],
		
		width:'100%',
		title:"自我总结列表",
		loadMask:true,
		pageSize: 5,
		dockedItems:[{
			dock:'top',
			xtype:'toolbar',//"<span style='color:red'>双击可以修改</span>",'-',	
			items:[{
				width:300,
				fieldLabel:'搜索',
				labelWidth:70,
				xtype:'searchfield',
				store:summaryStore			
			},{
				xtype:'label',
				html:'<div style="font-size:12px;color:red">标题模糊查询</div>'
			},{
				text:'修改',
				iconCls:'Applicationedit',
				xtype:'button',
				handler:function(){
					var selectedKeys = grid.getSelectionModel().getSelection();
					if(selectedKeys.length == 0){
						Ext.Msg.alert("提示", "请选中一条记录！");
						return;
					}
					newWindow(selectedKeys[0]);
				}
			},'-',{
				text:'刷新',
				iconCls:'ArrowRefresh',
				xtype:'button',
				handler:function(){
					grid.store.reload();
				}
			},'->',{
				text:'添加',
				iconCls:'Add',
				xtype:'button',
				handler:function(){
					newWindow(false);
				}
			},'-',{
				text:'删除',
				iconCls:'Decline',
				xtype:'button',
				handler:function(){
					var selectedKeys = grid.getSelectionModel().getSelection();
					if(selectedKeys.length == 0){
						Ext.Msg.alert("提示", "请选中一条记录！");
						return;
					}
					Ext.MessageBox.confirm('提示', '确定删除编号为'+selectedKeys[0].get("summaryId")+'的记录吗？', Delete); 
				}
			}]		
		},{
			dock:'bottom',
			xtype:'pagingtoolbar',	
			store:summaryStore,
			displayInfo:true,
			displayMsg:'显示{0}-{1}条，共计{2}条',
			emptyMsg:"没有数据"
		},{
			dock:'bottom',
			xtype:'label',
			html:'<div style="font-size:12px;color:red">*双击可查看详细信息（创建7天后不能修改）</div>'
		}],
		listeners:{  
			//双击事件
			itemdblclick : function( grid, record, item, index, e, eOpts){  
				var selectedKeys = grid.getSelectionModel().getSelection();
				showDetails(selectedKeys);
				
			       }  
		}
	});
	 
	function Delete(btn){
		if(btn == 'yes'){
			var selectedKeys = grid.getSelectionModel().getSelection();
			var summaryId=selectedKeys[0].get("summaryId");
			Ext.Ajax.request( {
	    		url : "deleteSummaryAction.action", 
	    		params : {
	    			summaryId : summaryId
	    		}, //参数
	    		success : function(response, option) {
	    			var obj = Ext.decode(response.responseText);
	    			if(obj.success){
	    				Ext.Msg.alert("提示", "删除成功！");
	    				grid.store.reload();
	    			}else{
	    				Ext.Msg.alert("提示", "删除失败！");
	    			}
	    			
	    		},
	    		failure : function(response, option) {
	    			Ext.Msg.alert("提示", "保存失败！");
	    		}
	    		});
		}else{
			return;
		}

	}
	
	function newWindow(record){
		/*if(!record){
			Ext.Msg.alert("提示","创建之后你有一周的时间可以对其修改，请谨慎填写！");
		}*/
		if (record!=null && record != false){
			var nowDate = new Date();
			var limitDate = record.get('modifyDateLimit');
			limitDate =limitDate.replace("T"," ").substring(0,16);
			//alert(limitDate+"***"+getNowFormatDate());
			if(comptime(limitDate,getNowFormatDate()) == false){
				Ext.Msg.alert("提示","修改期限已过，您不能对该条记录进行修改！");
				return;
			}
		//	myDate = myDate.format('yyyy-MM-dd h:m:s');
		//	alert(myDate +"***"+);
			/*if(myDate > limitDate){
				Ext.Msg.alert("提示","修改期限已过，您不能对该条记录进行修改！");
				return ;
			}*/
		}
		
		
		//combo
		 //权限下拉列表
	    var isVisibleBox =  Ext.create('Ext.form.field.ComboBox',{
	     	//extend: 'Ext.form.field.ComboBox',
	     	id:'isVisible',
	     	alias:'widget.lifechannelCombo',
	         width: 200,
	         fieldLabel: '权限',
	         store: isVisibleStore,
	         displayField: 'valueName',
	         valueField: 'valueId',
	         triggerAction: 'all',
	         emptyText: '请选择...',
	         blankText: '请选择专业',        
	         queryMode:'local'
	     });
		var editSummary = Ext.create('Ext.Window',{
			id:'editSummaryId',
			layout:'form',
			width:800,
			height:600,
			//closeAction:'hide',
			plain:true,
			modal: true,
			resizable : false,
			title:'我的总结',
			items:[{
				xtype:'textfield',
				fieldLabel:'编号',
				id:'summaryId',
				hidden:true,
				listeners: {
		               'afterrender': function () {
		            	   //Ext.Msg.alert("ss",record);
		                   if (record!=null && record != false){
		                	   this.setValue(record.get('summaryId'));
		                   }
		               }
		           }
			},{
					   xtype:'textfield',  
			           fieldLabel:'标题', 
			          // width:400,
			           id:'summaryTitle',
			           name:'title',  
			           allowBlank:false,  
			           blankText:'标题不能为空',
			           listeners: {
			               'afterrender': function () {
			            	   //Ext.Msg.alert("ss",record);
			                   if (record!=null && record != false){
			                	   this.setValue(record.get('title'));
			                   }
			               }
			           }
				},{
					xtype:'xhtmleditor',
	            	//width:500,
					id:'summaryText',
					//name:'text',
	        		height:400,
	        		fieldLabel: '内容',
	        		allowBlank:false,  
			        blankText:'内容不能为空',
			        uploadConfig:{
	                    url:"uploadPhotoByTypeAction.action?PicType=studentSummary"
	                },
	        		listeners: {
			               'afterrender': function () {
			                   if (record!=null && record != false){
			                	  this.setValue(record.get('contentText'));
			                   }else{
			                	   this.setValue('<span style="color:#aa0000;">创建之后你有一周的时间可以对其修改，请谨慎填写！</span>');
			                   }
			               }
			           }
			},{
				//xtype:'xhtmleditor',
            	//width:500,
				id:'remark',
				//name:'text',
        		height:50,
        		fieldLabel: '备注',
        		xtype     : 'textareafield',
                grow      : true,
                anchor    : '100%',
        		//allowBlank:false,  
		        //blankText:'内容不能为空',
        		listeners: {
		               'afterrender': function () {
		                   if (record!=null && record != false){
		                	  this.setValue(record.get('remark'));
		                   }
		               }
		           }
		},{
			xtype:'combo',
			id:'isVisible',
	     	alias:'widget.lifechannelCombo',
	         width: 20,
	         fieldLabel: '权限',
	         store: isVisibleStore,
	         displayField: 'valueName',
	         valueField: 'valueId',
	         triggerAction: 'all',
	         emptyText: '请选择导师查看权限...',
	         blankText: '请选择导师查看权限',        
	         queryMode:'local',
	         listeners: {
	               'afterrender': function () {
	                   if (record!=null && record != false){
	                	  this.setValue(record.get('visible'));
	                   }
	               }
	           }
		}],
			buttons : [ {
				xtype : "button",
				text : "保存",
				pressed : false,
				handler : validatorData
			}],
		buttonAlign : "center",
		plain:true
			
		});
		editSummary.show();
	}
	
	function validatorData(){ 
		
		var text = Ext.getCmp('summaryText').getValue();
		if(text == "" || text == null){
			Ext.Msg.alert("提示","内容不能为空");
			return;
		}
		var title = Ext.getCmp('summaryTitle').getValue();
		if(title == null || title == ""){
			Ext.Msg.alert("提示","标题不能为空");
			return;
		}
		var remark = Ext.getCmp('remark').getValue();
	
		var visible = Ext.getCmp('isVisible').getValue();
		if(visible == null || visible == ''){
			Ext.Msg.alert("提示","请选择导师查看权限！");
			return;
		}
		var id = Ext.getCmp('summaryId').getValue();
		var url = 'updateSummaryAction.action';
		if(id == null || id == ''){
			url = 'insertSummaryAction.action';
		}
		Ext.Ajax.request({  
	        url : url,  
	        params : {  
	        	summaryId : id,
	        	title:title,
	        	contentText:text,
	        	remark:remark,
	        	visible:visible
	        },  
	        method : 'POST',  
	        success : function(response, opts) {  
	            var success = Ext.decode(response.responseText).success;  
	            // 当后台数据同步成功时  
	            if (success) {  
	            	summaryStore.load();
	            	Ext.Msg.alert("提示","操作成功");
	            	Ext.getCmp('editSummaryId').close();
	            } else {  
	            	Ext.Msg.alert("提示",Ext.decode(response.responseText).msg);
	            }  
	        }  
	        
	    });  
	}
	
	function showDetails(data){
		
		var tabId = "summaryDetails" + data[0].get("summaryId");
    	var tabName = data[0].get("title").length > 15?data[0].get("title").substring(0,20):data[0].get("title");
    	var url = 'jsp/student/showSummary.jsp?';
    	url+="&studentId="+data[0].get("studentId");
    	url+="&summaryId="+data[0].get("summaryId");
    	var createDate = data[0].get("createDate");
    	createDate = createDate.replace("T"," ").substring(0,16);
    	/*url +="&title="+data[0].get("title");
    	url +="&createDate="+data[0].get("createDate");
    	url +='&contentText='+data[0].get("contentText");
    	url +="&remark="+data[0].get("remark");*/
    //	url.replace('"', '/'');
    	//url = url.replace(/\"/g,"'");
    //	alert(url);
		var tab = center.queryById(tabId);
	        if (!tab) {
	        	var tab = center.add({
	        		id: tabId,
	        		iconCls: 'Bookopen',       
	        		xtype: "panel",
	        		title: tabName,
	        		closable: true,
	        		//maskDiabled: true,
	        		frame: true,
	        		autoScroll : true,
	        		region:center,
	        		//baseCls:'allBackImage',
	        		bodyStyle: 'background:white;',
	        		//html:'<iframe scrolling="auto" frameborder="0" width="100%" height="100%" src="'+url+'"></iframe>'
	        		html:'<center><h2>'+data[0].get("title")+'</h2><span style="font-size:12px;"> 创建时间：'+createDate+'</span><hr style="width:90%;"></center><div style="margin:auto;width:90%;">'+data[0].get("contentText")+'</div><br><br><br><br><div style="margin:auto;width:90%;font-size:12px;">备注：'+data[0].get("remark")+'</div>'
	        	});
	        	
	        }
	      /*  var remark = data[0].get("remark")==''?'无':data[0].get("remark");
	        Ext.getCmp(tabId).update('<div style="width:90%;padding-left:80px"><h2 style="text-align:center">'+data[0].get("title")+'</h2>'+
 	        		'<div style="text-align:center">创建时间：'+data[0].get("createDate").replace("T"," ")+'</div>'+
 	        		'<div style="font-size:15px;text-indent:2em">'+data[0].get("contentText")+'</div>'+
 	        		'<div style="font-size:15px;text-align:right"><br></div><br><br><br><br><br>'+
 	        		'<div style="font-size:10px">备注：'+remark+'</div>'+'</div>');*/
    	center.setActiveTab(tabId);
	}
	 return grid; 
}